<template>
  <div>
    <!-- 添加 -->
    <el-form
      :model="form"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="姓名" prop="FullName">
            <el-input v-model="form.FullName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="用户名" prop="UserName">
            <el-input v-model="form.UserName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="邮箱" prop="Email">
            <el-input v-model="form.Email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="头像" prop="Avatar">
            <el-upload
              class="avatar-uploader"
              :action="UploadApi"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { Update, Backfill } from "../../apis/admin";
export default {
  //数据
  data() {
    return {
      form: {
        AdminId: 0,
        FullName: "",
        UserName: "",
        Email: "",
        Avatar: "",
      },
      rules: {
        FullName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        UserName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],

        Email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
      UploadApi: `${process.env.VUE_APP_BASEURL}UploadImage/UploadAvatar`,
      imageUrl: "",
    };
  },

  props: {
    adminId: {
      type: Number,
      default: 0,
    },
  },

  //方法
  methods: {
    //添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this;
          Update(this.form).then((res) => {
            if (res.Code == 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                onClose: function () {
                  _this.$emit("onAddSuccess");
                },
              });
            } else {
              this.$message.error(res.Message);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //文件上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.Avatar = res.fileName;
    },
    //文件上传前的回调
    beforeAvatarUpload(file) {
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPNG) {
        this.$message.error("上传头像图片只能是 PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isPNG && isLt2M;
    },
  },
  //勾子函数
  mounted() {
    //反填
    Backfill({ AdminId: this.adminId }).then((res) => {
      this.form = res.Data;
      this.imageUrl = `${process.env.VUE_APP_BASEURL.replace("/api", "")}${
        res.Data.Avatar
      }`;
    });
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 16px;
  color: #8c939d;
  width: 55px;
  height: 55px;
  line-height: 55px;
  text-align: center;
}
.avatar {
  width: 55px;
  height: 55px;
  display: block;
}
</style>